package edu.pku.sei.modeler.uml.commands.sequence;


import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.ExecutionSpecification;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.Fragments.CombinedFragment;
import edu.pku.sei.modeler.model.uml.Fragments.InteractionUse;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;
import edu.pku.sei.modeler.uml.commands.UMLCommentLinkCreateCommand;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommand;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommandFactory;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;

public class SequenceDiagramConnectionCreateFactory extends
		UMLConnectionCreateCommandFactory {

    public static SequenceDiagramConnectionCreateFactory INSTANCE = new SequenceDiagramConnectionCreateFactory();
    
    private SequenceDiagramConnectionCreateFactory() {
    	
    }
	
	public UMLConnectionCreateCommand createCommand(MetaLink link, MetaNode source) {
		MetaModelElement sourceModelElement = source.getModelElement();
		UMLConnectionCreateCommand cmd = new UMLConnectionCreateCommand(link, source);
		if (sourceModelElement instanceof ExecutionSpecification) {
			cmd.setModelCmd(new UMLMessageCreateCommand(link.getModelElement(), source.getModelElement()));
		} else if (sourceModelElement instanceof Lifeline) {
			cmd.setModelCmd(new UMLMessageCreateCommand(link.getModelElement(), source.getModelElement()));
//		} else if (sourceModelElement instanceof Comment) {
//			if (link.getModelElement() instanceof Dependency) {
//				cmd.setModelCmd(new UMLCommentLinkCreateCommand(link.getModelElement(), source.getModelElement()));
//			}
		} else {
			cmd = null;
		}
		return cmd;
	}
	
	public UMLConnectionCreateCommand completeCommand(
			UMLConnectionCreateCommand command, MetaNode target) {
//		MetaModelElement linkModel = command.getLink().getModelElement();
//		MetaModelElement targetModel = target.getModelElement();
//		if (targetModel instanceof Comment
//				|| targetModel instanceof Interaction
//				|| targetModel instanceof Lifeline
//				|| targetModel instanceof CombinedFragment
//				|| targetModel instanceof InteractionUse) {
//			if (linkModel instanceof Dependency) {
//				command.setTarget(target);
//			}
//		}
//		if (command.getTarget() == target)
//			return command;
//		else
			return null;
	}
	
	public SequenceReconnectSourceCommand 
	createReconnectSourceCommand(MetaLink link, int newOffset) {
		return new SequenceReconnectSourceCommand(link, newOffset);
	}
	
	public SequenceReconnectTargetCommand 
	createReconnectTargetCommand(MetaLink link, int newOffset, int newDelay) {
		return new SequenceReconnectTargetCommand(link, newOffset, newDelay);
	}

}
